Page({
  /**
   * 页面的初始数据
   */
  data: {
    title: "易税工场",
    is_bind: false,
    is_contract: false,
    is_auth: false,
    list: [],
    options: null,
    QRCodeType: '',
    natural_status: '',
    is_professional_notice: '',
    is_natural_notice: '',
    canIUse_edit: true,
  },
  scope_userInfo() {
    let that = this;
    wx.getSetting({
      success: res => {
        console.log(res, res.authSetting['scope.userInfo'], '::::::::::::Res')
        that.setData({
          canIUse_edit: res.authSetting['scope.userInfo'] || false
        })
      }
    })
  },
  changeCard: function (e) {
    let {
      id
    } = e.currentTarget.dataset || {};
    this.scope_userInfo();
    if (this.data.canIUse_edit) {
      wx.navigateTo({
        url: '/pages/changeCard/changeCard?id=' + id,
      })
    }
  },
  goAddMaterial() {
    this.scope_userInfo();
    if (this.data.canIUse_edit) {
      wx.navigateTo({
        url: '/pages/uploadCerface/uploadCerface',
      })
    }
  },
  toEvaluate: function () {
    this.scope_userInfo();
    if (this.data.canIUse_edit) {
      wx.navigateTo({
        url: '/pages/evaluate/evaluate',
      })
    }
  },
  newSigned: function () {
    this.scope_userInfo();
    if (this.data.canIUse_edit) {
      wx.navigateTo({
        url: '/pages/newSigned/newSigned',
      })
    }
  },
  // 选择服务商
  select_facilitator: function (e) {
    let {
      id
    } = e.currentTarget.dataset || {};
    this.scope_userInfo();
    if (this.data.canIUse_edit) {
      wx.navigateTo({
        url: '/pages/selectFacilitator/selectFacilitator?id=' + id,
      })
    }
  },
  // 去签约
  toSign: function (e) {
    let {
      id
    } = e.currentTarget.dataset || {};
    this.scope_userInfo();
    if (this.data.canIUse_edit) {
      wx.navigateTo({
        url: '/pages/business/business?id=' + id,
      })
    }
  },
  //去签署详情
  toMy(e) {
    let {
      id,
      eid
    } = e.currentTarget.dataset || {};
    this.scope_userInfo();
    if (this.data.canIUse_edit) {
      wx.navigateTo({
        url: '/pages/signingDetails/signingDetails?id=' + id + '&enterprise_id=' + eid,
      })
    }
  },
  // 查看签约
  toSign1(e) {
    let {
      id
    } = e.currentTarget.dataset || {};
    this.scope_userInfo();
    if (this.data.canIUse_edit) {
      wx.navigateTo({
        url: '/pages/business/business?id=' + id,
      })
    }
  },
  // 签署报告
  sign_report(e) {
    let {
      id
    } = e.currentTarget.dataset || {};
    this.scope_userInfo();
    if (this.data.canIUse_edit) {
      wx.navigateTo({
        url: '/pages/signedReport/signedReport?id=' + id,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.scene) {
      console.log(options.scene, ':::::::::::::options.scene')
      wx.setStorageSync('invitationCode', options.scene);
      console.log(options.scene.match(/^a|^zy/i) ? "naturalPerson" : "freelancers")
      wx.setStorageSync('QRCodeType', options.scene.match(/^a|^zy/i) ? "naturalPerson" : "freelancers");
    }

    this.setData({
      options: options
    })
  },
  userInfo(data) {
    let that = this;
    // console.log(data);
    if (data.detail) {
      wx.$api.saveUserInfo({
        head_img: data.detail.avatarUrl,
        nickname: data.detail.nickName
      }).then(res => {
        console.log(res);
        if (res.data.code == 200) {
          that.setData({
            canIUse_edit: true
          })
        }
      })
    }
  },
  async indexLoad(options) {
    let that = this;
    let is_bind = wx.getStorageSync('is_bind');
    let is_contract = wx.getStorageSync('is_contract');
    let is_auth = wx.getStorageSync('is_auth');
    let QRCodeType = wx.getStorageSync('QRCodeType');
    let natural_is_auth = wx.getStorageSync('natural_is_auth');
    let natural_is_contract = wx.getStorageSync('natural_is_contract');
    let natural_status = wx.getStorageSync('natural_status');
    console.log('come indexlodad', is_bind, is_contract, is_auth);
    let is_natural_notice = getApp().globalData.is_natural_notice;
    let is_professional_notice = getApp().globalData.userInfo.is_professional_notice;
    wx.showLoading({
      title: '加载中...',
      mask: true,
      icon: "none"
    })
    that.setData({
      is_bind: is_bind,
      is_contract: is_contract,
      is_auth: is_auth,
      QRCodeType: QRCodeType,
      natural_status: natural_status,
      is_natural_notice: is_natural_notice,
      is_professional_notice: is_professional_notice
    })
    let to_page = (url) => {
      wx.reLaunch({
        url: url,
      })
    }
    console.log(123, ':::::::::123', QRCodeType, natural_is_auth, natural_is_contract)
    if (QRCodeType == 'naturalPerson') {
      if (natural_status == 1) {
        wx.showModal({
          title: '提示',
          content: '该自然人账号已被冻结',
          showCancel: false,
          success(res) {
            to_page('/pages/freezePage/freezePage')
          }
        })
      }
      if (natural_is_auth == 0) {
        to_page('/pages/personalDetails/personalDetails');
      } else if (natural_is_contract == 0) {
        to_page('/pages/business/business?id=' + '1');
      } else if (natural_is_auth == 1) {
        // to_page('');
        wx.hideLoading()
      }
    } else {
      if (!is_bind) {
        //由于app.js 中调getopenid 的接口和进入 index 页面是一个异步过程，所以这里要做相应处理
        let go_url = '/pages/login/login';
        if (options.scene != undefined) { //如果带了参数过来
          go_url = go_url + '?scene=' + options.scene;
        }
        to_page(go_url);
      } else if (!is_auth) {
        to_page('/pages/personalDetails/personalDetails');
      } else if (!is_contract) {
        if (options.scene != undefined) {
          let go_url = '/pages/newSigned/newSigned?codes=' + options.scene;
          to_page(go_url);
        } else {
          to_page('/pages/newSigned/newSigned');
        }
      } else {
        if (options.scene != undefined) {
          let go_url = '/pages/newSigned/newSigned?codes=' + options.scene;
          to_page(go_url);
        } else {
          let data = await wx.$api.myContractList({});
          that.setData({
            list: data.data.data || []
          })
        }
      }
    }


  },

  async wxLogin() {
    await getApp().getOpenId();
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.scope_userInfo();
    this.init(this.data.options || {});
    this.setData({
      QRCodeType: wx.getStorageSync('QRCodeType')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  async init(options) {
    let that = this;
    await that.wxLogin();
    await that.indexLoad(options);
  },
  tabbarClick(data) {
    console.log(data)
    this.scope_userInfo();
    if (this.data.canIUse_edit) {
      if (data.type == 0 || data.type == 1) {
        wx.reLaunch({
          url: data.detail.path
        })
      } else {
        wx.reLaunch({
          url: data.detail.path
        })
      }
    }
  },
  goOpenRecord() {
    this.scope_userInfo();
    if (this.data.canIUse_edit) {
      wx.navigateTo({
        url: '/pages/openRecord/openRecord',
      })
    }
  },
  goInvoicePage() {
    this.scope_userInfo();
    if (this.data.canIUse_edit) {
      wx.navigateTo({
        url: '/pages/invoicingApplication/invoicingApplication',
      })
    }
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})